/*
 * ldap_protocol.h
 *
 *    Author: Matias Dumrauf
 * Revisions:
 *               - Bug Fixes & better implementation (17/06/2010).
 *				 - News NetWork System               (16/04/2010).
 *				 - SadicO                            (02/11/2009).
 *				 - SOogle (Base Revision)            (01/07/2009).
 */

#ifndef LDAP_PROTOCOL_H_
#define LDAP_PROTOCOL_H_

	#include <windows.h>
	#include <ldap.h> /* ldap_err2string */

	#define DEFAULT_NEWSGROUP_LENGTH     50
	#define DEFAULT_LOGIN_DN_LENGTH      30
	#define DEFAULT_PASSWORD_LENGTH      30
	#define DEFAULT_IP_LENGTH            16

	/* Attribute's Name Macros */
	#define ATTRIBUTE_OBJECT_CLASS          "objectClass"
	#define ATTRIBUTE_NEWSGROUP_NAME        "newsgroupName"
	#define ATTRIBUTE_NUM_OF_NEWS           "numOfNews"
	#define ATTRIBUTE_NEWS_ID               "newsId"
	#define ATTRIBUTE_NEWS_HEAD             "newsHead"
	#define ATTRIBUTE_NEWS_BODY             "newsBody"

	typedef enum _attribute_type{
		IS_ATTRIBUTE_OBJECT_CLASS, IS_ATTRIBUTE_NEWSGROUP_NAME, IS_ATTRIBUTE_NUM_OF_NEWS,
		IS_ATTRIBUTE_NEWS_ID, IS_ATTRIBUTE_NEWS_HEAD, IS_ATTRIBUTE_NEWS_BODY
	} e_attribute;


	/* Branches de Directorios Macros */
	#define BRANCH_NEWSNETWORK              "ou=newsnetwork,dc=utn,dc=edu"

	/* ObjectClass Structure Macros */
	#define OBJECT_CLASS_TOP                "top"
	#define OBJECT_CLASS_NEWSGROUP          "newsgroup"
	#define OBJECT_CLASS_NEWS               "news"

	typedef struct _ldap_newsgroup{
		char  newsgroup_name[DEFAULT_NEWSGROUP_LENGTH];
		int   num_of_news;
	} t_ldap_newsgroup;

	typedef struct _ldap_news{
		char  newsgroup_name[DEFAULT_NEWSGROUP_LENGTH];
		long  id;
		char *head;
		char *body;
	} t_ldap_news;

	typedef struct _ldap_context{
		char login_dn[DEFAULT_LOGIN_DN_LENGTH];
		char password[DEFAULT_PASSWORD_LENGTH];
		char ip[DEFAULT_IP_LENGTH];
		int  port;
	} t_ldap_context;


	t_ldap_context   *ldap_protocol_createContext(HANDLE currentHeap, char *ip, int port, char *login_dn, char *password);
	void              ldap_protocol_destroyContext(HANDLE currentHeap, t_ldap_context *ldap_ctx);

	#define           ldap_protocol_getErrorDescription(ldaperror)    ldap_err2string(ldaperror)


	int               ldap_protocol_newsgroup_add(HANDLE currentHeap, t_ldap_context *ctx, char newsgroup_name[]);
	int               ldap_protocol_newsgroup_delete(HANDLE currentHeap, t_ldap_context *ctx, char newsgroup_name[]);
	int               ldap_protocol_newsgroup_exists(HANDLE currentHeap, t_ldap_context *ctx, char newsgroup_name[]);
	int               ldap_protocol_newsgroup_select(HANDLE currentHeap, t_ldap_context *ctx, char newsgroup_name[], /*inout*/t_ldap_newsgroup **newsGroupSelected);
	void              ldap_protocol_newsgroup_destroy(HANDLE currentHeap, t_ldap_newsgroup *newsgroup);

	int               ldap_protocol_news_add(HANDLE currentHeap, t_ldap_context *ctx, char newsgroup_name[], long newsId, char *head, char *body);
	int               ldap_protocol_news_delete(HANDLE currentHeap, t_ldap_context *ctx, char newsgroup_name[], long newsId);
	int               ldap_protocol_news_exists(HANDLE currentHeap, t_ldap_context *ctx, char newsgroup_name[], long newsId);
	int               ldap_protocol_news_select(HANDLE currentHeap, t_ldap_context *ctx, char newsgroup_name[], long newsId, /*inout*/t_ldap_news **newsSelected);
	void              ldap_protocol_news_destroy1(HANDLE currentHeap, t_ldap_news *news);
	void              ldap_protocol_news_destroy2(HANDLE currentHeap, t_ldap_news *news);

#endif /* LDAP_PROTOCOL_H_ */
